#include <iostream>
#include <stdio.h>
#include <cmath>
#include <GL/glut.h>
#include "LinAlg/LinAlg.h"
#include "SceneGraph/SceneGraph.h"
#include "Utils/trackball.h"
#include "Utils/FPS.h"
#include "Tower.h"
#include "Tower2.h"
#include "Texture/Texture.h"
Texture *tex = new Texture();
#include "Utils/keyboard.h"

using namespace std;

float width = 800, height = 600;
Matrix4* i = new Matrix4();
FrustrumCulling* FC = new FrustrumCulling();
Tower *t = new Tower();
Tower2 *t2= new Tower2();
Matrix4 *flip = new Matrix4();


void displayCallback(){
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // clear color and depth buffers
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(Xval,Yval,Zval);
	glScalef(0.3, 0.3, 0.3);
	i->identity();
	i->multiply(rot->get());
	i->multiply(*flip);
	if(firstTower)
		t->draw(i, tex);
	else
		t2->draw(i, tex);
	
	FPS();
	glutSwapBuffers();
}

void reshapeCallback(int w, int h){
  width = w;
  height = h;
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  float fov = 60, aspect = 1, near = 10, far = 1000;
	// FOV, Aspect, Near, Far
	gluPerspective(fov, aspect, near, far);
	// px, py, pz  lx,ly,lz  ux,uy,uz
	Vector3 p; p.set(0, 0, 0);  
	Vector3 l; l.set(0, 0, -1);  
	Vector3 u; u.set(0, 1, 0);
	gluLookAt(p[0], p[1], p[2],
						l[0], l[1], l[2],
						u[0], u[1], u[2]);
}

void idleCallback(){
	displayCallback();
}

int main(int argc, char* argv[]){
  glutInit(&argc, argv);      	      	
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
  glutInitWindowSize(width, height);    
  glutCreateWindow("Bezier tower!");    	              
  glEnable(GL_TEXTURE_2D);   // enable texture mapping
  glEnable(GL_DEPTH_TEST);            	     
  glClearColor(0.0, 0.0, 0.0, 1.0);   
  
  // Callbacks
  glutDisplayFunc(displayCallback);
  glutReshapeFunc(reshapeCallback);
  glutIdleFunc(idleCallback);
  glutKeyboardFunc(keyboardCallback);
  glutMouseFunc(mouseFunc);
  glutMotionFunc(motionFunc);
  
  // Initialize
  reshapeCallback(width, height);
  trackballInit(width, height);
  flip->rotationX(180);
  tex->loadTexture();
  Xval = 0;
  Yval = 80;
  Zval = -300;
  /* light
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_NORMALIZE);
  glEnable(GL_COLOR_MATERIAL);
  */
  
  glutMainLoop();
  return 0;
}
